import {Component, EventEmitter, OnInit, Output} from '@angular/core';
import {BaseService} from "../../../../shared/service/base.service";
import {LOG} from "../../../../../../_mock/log";
import {ApiUrl} from "../../../../config/api-url";
import {Consts} from "../../../../config/consts";
import {PageHelper} from "../../../../utils/page-helper-model";
import {RoleIdEnum} from '../../../../enums/role-id.enum';
import {UserInfoModel} from '../../../../models/user-info.model';

@Component({
  selector: 'app-log-system',
  templateUrl: './log-system.component.html',
  styleUrls: ['./log-system.component.less']
})
export class LogSystemComponent implements OnInit {
  isVisible = false;
  param:any;
  listOfData: any;
  // 分页
  pageHelper: PageHelper = new PageHelper;
  @Output()
  event: EventEmitter<any> = new EventEmitter();

  setOfCheckedId = new Set<number>();
  // 选中
  checked = false;
  roleIdEnum = {
    SUPER_ADMINISTRATOR: RoleIdEnum.SUPER_ADMINISTRATOR,
    AGENT: RoleIdEnum.AGENT,
    MERCHANT: RoleIdEnum.MERCHANT,
  };
  // 用户信息
  userInfo: UserInfoModel;
  constructor(public baseService: BaseService) {
  }

  ngOnInit(): void {
    this.getAgentsData();
    this.userInfo = JSON.parse(sessionStorage.getItem(Consts.USER_INFO));
  }

  getAgentsData() {
    // this.listOfData = LOG;
    this.baseService.findListByKeyword(ApiUrl.LogManipulate + '/list', this.pageHelper).subscribe(
      data => {
        if (data.status == 0) {
          this.listOfData = data.data.list;
          this.pageHelper.totalItems = data.data.total;
          this.pageHelper.numPages = data.data.pages;
        } else {
          this.baseService.layer.messageError(data.msg);
        }
      }
    );
  }

  // 上、下页
  nzPageHelperChange($event: number) {
    this.pageHelper.currentPage = $event;
    this.getAgentsData();
  }

  // 刷新
  loadData() {
    this.pageHelper.currentPage = 1;
    this.getAgentsData();
  }
// 根据keyword条件获取列表
  findListByKeyword(keyword: any) {
    this.pageHelper.keyword = keyword;
    this.pageHelper.currentPage = 1;
    this.getAgentsData();
  }
  // // 删除
  // delete(id: any) {
  //   this.baseService.layer.modalConfirm('确定要删除该记录吗?', result => {
  //     if (result) {
  //       this.baseService.delete(ApiUrl.LogManipulate, id).subscribe(
  //         data => {
  //           if (data.status == 0) {
  //             this.baseService.layer.messageSuccess('删除成功！');
  //             this.loadData();
  //           } else {
  //             this.baseService.layer.messageError('删除失败：' + data.msg);
  //           }
  //         }
  //       );
  //     }
  //   });
  // }
  //
  // // 根据时间段来删除
  // deleteInStartAndEndDateTime(startDateTime, endDateTime) {
  //   let param = {
  //     startDateTime: startDateTime,
  //     endDateTime: endDateTime
  //   }
  //   console.log(param)
  //   this.baseService.post(ApiUrl.LogManipulate + '/deleteInStartAndEndDateTime', param).subscribe(
  //     data => {
  //       if (data.status == 0) {
  //         this.baseService.layer.messageSuccess('删除成功！');
  //         this.loadData();
  //         let body = {'deleteInDateTimeSuccess': true}
  //         this.event.emit(body);
  //       } else {
  //         this.baseService.layer.messageError('删除失败：' + data.msg);
  //       }
  //     }
  //   );
  // }
  //
  //
  // // 批量删除
  // batchDeleteInIds() {
  //
  //   if (this.setOfCheckedId.size == 0){
  //     this.baseService.layer.messageError('删除集合为空');
  //     return;
  //   }
  //
  //   this.baseService.layer.modalConfirm('确定要批量删除这些记录吗?', result => {
  //     if (result) {
  //
  //       // 组装成数组传给后端
  //       let array = new Array();
  //       this.setOfCheckedId.forEach(item => {
  //         array.push(item);
  //       })
  //
  //       let param = {
  //         batchDeleteToList:array
  //       }
  //       this.baseService.post(ApiUrl.LogManipulate + '/batchDeleteInIds', param).subscribe(
  //         data => {
  //           if (data.status == 0) {
  //             this.baseService.layer.messageSuccess('删除成功！');
  //             this.loadData();
  //           } else {
  //             this.baseService.layer.messageError('删除失败：' + data.msg);
  //           }
  //         }
  //       );
  //     }
  //   });
  // }
  showModal(data){
    this.isVisible = true;
    this.param = data.param;
  }
  handleCancel(){
    this.isVisible = false;
  }
}
